﻿Ext.define('app.system.plantpl.Index', {
    extend: 'Ext.tree.Panel',
    border: false,
    useArrows: true,
    rootVisible: false,
    //tbar: [],
    initComponent: function () {
        var me = this;
        me.store = new Ext.data.TreeStore({
            root: { expanded: true },
            autoLoad: true,
            nodeParam: 'parentId',
            // 根节点的参数值是0
            defaultRootId: "0",
            proxy: {
                type: 'ajax',
                reader: {
                    type: 'json',
                    rootProperty: function (node) {
                        return node.result || node.children;
                    }
                },
                url: '/api/services/app/planTpl/GetTree'
            }
        });
        me.tbar = [
            "->",
            {
                text: '新增', icon: '/Content/images/add.png', handler: function () {
                    var win = Ext.create("app.system.plantpl.Create");
                    win.on("savesuccess", function () {
                        me.store.load();
                        win.close();
                    });
                    win.show();
                }
            },
            {
                text: '保存修改', icon: '/Content/images/page_save.png', handler: function () {
                    var arry = [];
                    var store = me.store;
                    for (var i = 0; i < store.getCount() ; i++) {
                        var record = store.getAt(i);
                        arry.push({ id: record.data.id, name: record.data.text, day: record.data.day });
                    }
                    if (arry.length == 0) {
                        Ext.Msg.alert('系统提示', "没有需要保存的数据");
                        return false;
                    }
                    App.AjaxPost('/api/services/app/plantpl/Edit', arry, function (data) {
                        me.store.load();
                    });
                }
            }
        ];
        me.callParent();
    },
    plugins: [
        Ext.create('Ext.grid.plugin.CellEditing', { clicksToEdit: 1 })
    ],
    columns: [{
        xtype: 'treecolumn',
        header: '名称',
        width: 200,
        sortable: false,
        dataIndex: 'text',
        editor: { xtype: 'textfield' }
    },
    {
        header: '工期',
        sortable: false,
        dataIndex: 'day',
        editor: { xtype: 'textfield' }
    }],
    root: { id: '0', text: '' },
    listeners: {
        itemcontextmenu: function (menutree, record, item, index, e, eOpts) {
            e.preventDefault();
            e.stopEvent();
            var nodemenu = new Ext.menu.Menu({
                floating: true,
                items: [
                {
                    icon: '/Content/images/add.png',
                    text: "新增子项",
                    hidden: record.data.leaf,
                    handler: function () {
                        var win = Ext.create("app.system.plantpl.CreateItem", { planTplId: record.data.id });
                        win.on("savesuccess", function () {
                            menutree.store.load();
                            win.close();
                        });
                        win.show();
                    }
                },
                {
                    icon: '/Content/images/delete.gif',
                    text: "删除阶段",
                    handler: function () {
                        Ext.Msg.confirm('系统提示', '确定要删除模板吗？', function (btn) {
                            if (btn == 'yes') {
                                App.AjaxDelete('/api/services/app/plantpl/Delete?id=' + record.data.id, function (data) {
                                    menutree.store.load();
                                });
                            }
                        });
                    }
                }]
            });
            nodemenu.showAt(e.getXY());
        },
        afterrender: function (tree) { tree.expandAll(); }
    }
})